package com.woniu.zsd.permission.controller;

import com.woniu.zsd.permission.Permission.Permission;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.permission.entity.Resource;
import com.woniu.zsd.permission.entity.RolesResource;
import com.woniu.zsd.permission.service.IResourceService;
import com.woniu.zsd.permission.utils.RoleResourceCacheUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理")
public class ResourceController {
    @Autowired
    private IResourceService resourceService;

    @GetMapping("/selResource")
    @ApiOperation(value = "查询所有资源")
    public Result<List<Resource>> selResource(@RequestHeader String token){
        List<Resource> resources = resourceService.selResource();
        return Result.getListResult(Resource.class).setCode(200).setMsg("查询所有资源成功").setData(resources);
    }

    @PutMapping("/addResource")
    @ApiOperation(value = "添加资源")
    @Permission("addResource")
    public Result addResource(@ApiParam(name = "name", value = "资源名称")@RequestParam String name, @ApiParam(name = "name", value = "资源描述")@RequestParam String resourceDesc, @RequestHeader String token){
        resourceService.addResource(name,resourceDesc);
        return Result.getResult();
    }

    @PostMapping("/updateResource")
    @ApiOperation(value = "修改资源")
    @Permission("updateResource")
    public Result updateResource(@ApiParam(name = "Id", value = "资源id")@RequestParam Integer resourceId,
            @ApiParam(name = "name", value = "资源名称")@RequestParam String name, @ApiParam(name = "name", value = "资源描述")@RequestParam String resourceDesc,@RequestHeader String token){
        resourceService.updateResource(resourceId,name,resourceDesc);
        return Result.getResult();
    }

    @DeleteMapping("/deleteResource")
    @ApiOperation(value = "删除资源")
    @Permission("deleteResource")
    public Result deleteResource(@RequestParam Integer resourceId,@RequestHeader String token){
        resourceService.deleteResource(resourceId);
        return Result.getResult();
    }
}